Ext.ns('Portal.contactForm');

Ext.apply(Portal.contactForm, {

    fnBringContact: function(oFpAdd, contact) {

        Ext.Ajax.request({
            url: "Contact/GetContactById",
            params: {
                person_id: contact.id
            },
            success: function (response) {
                var oRes = Ext.decode(response.responseText);
                var oData = oRes.Records;
                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
            }
        });
    }
});

Ext.apply(Portal.contactForm, {

    exec: function(contact) {

        // first check for unique id
        if (!Ext.getCmp('contact_' + contact.id)) {

            var formCompany = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Company',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdCompany",
                itemId: "company",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    readerFields: [
                        { name : "Id",  mapping : "IdCompany"  },
                        { name : "Name", mapping : "CompanyName" }
                    ]  ,
                    baseParams: {},
                    proxyUrl: 'Company/GetCompany/'
                } ,
                listeners: {
                    select:  function (combo, value) {
                        var cbBranch = fpContact.find("itemId", "branch")[0];
                        cbBranch.setValue('');
                        cbBranch.setDisabled(false);
                        cbBranch.store.load({ params : {
                           IdCompany : combo.getValue()
                        }
                        });
                    }
                }
            });

            var formBranch = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Sucursala',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdBranch",
                itemId: "branch",
                disabled : true,
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    readerFields: [
                        { name : "Id",  mapping : "IdBranch"  },
                        { name : "Name", mapping : "BranchName" }
                    ]  ,
                    baseParams: {},
                    proxyUrl: 'Branch/GetBranch/'
                }
            });
            var formDomain = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Domeniu',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdDomain",
                itemId: "domain",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {
                        table: "domains"
                    },
                    proxyUrl: 'Domain/GetDomain/'
                },
                listeners: {
                    select:  function (combo, value) {
                        var cbSubDomain = fpContact.find("itemId", "subdomain")[0];
                        cbSubDomain.setValue('');
                        cbSubDomain.setDisabled(false);
                        cbSubDomain.store.load({ params : {
                            domain_id : combo.getValue()
                        }
                        });
                    }
                }
            });
            var formSubDomain = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Subdomeniu',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdSubdomain",
                itemId: "subdomain",
                disabled : true,
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {},
                    proxyUrl: 'Subdomain/GetSubdomain/'
                }
            });

            var formPhone = new Ext.form.TextField({               
                name: 'PhoneNumber',
                xtype : "textfield",
                fieldLabel: 'Telefon Fix',
                anchor: '80%'
            });
            var formMobil = new Ext.form.TextField({               
                name: 'MobilNumber',
                xtype : "textfield",
                fieldLabel: 'Telefon Mobil',
                anchor: '80%'
            });
            var formLName = new Ext.form.TextField({
                name: 'LastName',
                xtype : "textfield",
                fieldLabel: 'Nume',
                anchor: '80%'
            });
              var formFName = new Ext.form.TextField({                              
                name: 'FirstName',
                xtype : "textfield"   ,
                fieldLabel: 'Prenume',
                anchor: '80%'
            });
            var formSex = new Ext.ux.fastCombo.Combo({

                fieldLabel : "Sex",
                xtype : "uxFCombo",
                editable : false,
                name : "Sex",
                easyConfig: {
                    mode : "local",
                    localData: [
                        ['1', 'F'],
                        ['2', 'M']
                    ]
                }
            });
            var formBirth = new Ext.form.DateField({
                name: 'birth_date',
                fieldLabel: 'Zi Nastere',
                anchor: '80%',
                format: 'Y-m-d',
                anchor: '80%'
            });
            var formAge = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Varsta',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdAge",
                itemId: "age",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {
                        table_name:'age'
                    },
                    proxyUrl: 'Nom/GetNom/'
                }
            });
            var formIncome = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Venit',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdIncome",
                itemId: "income",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {
                        table_name:'income'
                    },
                    proxyUrl: 'Nom/GetNom/'
                }
            });
            var formEmail = new Ext.form.TextField({
                name: 'Email1',
                xtype : "textfield",
                fieldLabel: 'Adresa Email',
                anchor: '80%'
            });
            var formEmail2 = new Ext.form.TextField({
                name: 'Email2',
                xtype : "textfield",
                fieldLabel: 'Adresa Email 2',
                anchor: '80%'
            });
            var formWeb = new Ext.form.TextField({
                name: 'WebAddress',
                xtype : "textfield",
                fieldLabel: 'Adresa Web',
                anchor: '80%'
            });
            var formPosition = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Functie',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdPosition",
                itemId: "position",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {},
                    proxyUrl: 'Position/GetPosition/'
                },
                listeners: {
                    select:  function (combo, value) {
                        var cbSubPos = fpContact.find("itemId", "subposition")[0];
                        cbSubPos.setValue('');
                        cbSubPos.setDisabled(false);
                        cbSubPos.store.load({ params : {
                            position_id : combo.getValue()
                        }
                        });
                    }
                }
            });
            var formSubPosition = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Subfunctie',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdSubposition",
                itemId: "subposition",
                disabled : true,
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {},
                    proxyUrl: 'Subposition/GetSubposition/'
                }
            });
            var formDivision = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Departament',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdDivision",
                itemId: "dept",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {},
                    proxyUrl: 'Division/GetDivision/'
                }
            });
            var formPriority = new Ext.ux.fastCombo.Combo({
                fieldLabel: 'Prioritate',
                xtype: "uxFCombo",
                anchor: "70%",
                name: "IdPriority",
                itemId: "priority",
                forceSelection: false,
                minChars: 2,
                easyConfig: {
                    baseParams: {
                        table_name:'priority'
                    },
                    proxyUrl: 'Nom/GetNom/'
                }
            });
            var formObs = new Ext.form.TextArea({
                name: 'obs',
                xtype : "textfield",
                fieldLabel: 'Observatii',
                anchor: '80%',
                height: 60
            });
            var formCreated = new Ext.BoxComponent({
                fieldLabel: 'Creat la data',
                cls: 'x-form-text-mtp',
                ctCls: 'blue1-row',
                id: 'fpContact_created_date_' + contact.id,
                autoEl: {
                    tag: 'div',
                    html: '-'
                }
            });
            var formCreatedUser = new Ext.BoxComponent({
                fieldLabel: 'Creat de',
                cls: 'x-form-text-mtp',
                ctCls: 'blue1-row',
                id: 'fpContact_created_user_' + contact.id,
                autoEl: {
                    tag: 'div',
                    html: '-'
                }
            });
            var formModify = new Ext.BoxComponent({
                fieldLabel: 'Ultima modificare',
                cls: 'x-form-text-mtp',
                ctCls: 'blue3-row',
                id: 'fpContact_date_modified_' + contact.id,
                autoEl: {
                    tag: 'div',
                    html: '-'
                }
            });
            var formModifyUser = new Ext.BoxComponent({
                fieldLabel: 'Ultima modificare user',
                cls: 'x-form-text-mtp',
                ctCls: 'blue3-row',
                id: 'fpContact_user_modified_' + contact.id,
                autoEl: {
                    tag: 'div',
                    html: '-'
                }
            });
            var iconClsContact;

            if (contact.sex == '2') {
                iconClsContact = 'icon-user-suit';
            }
            if (contact.sex == '1') {
                iconClsContact = 'icon-user-female';
            }
            if (contact.sex == '3') {
                iconClsContact = 'icon-help';
            }

            var fpContact = new Ext.form.FormPanel({
                monitorValid: true,
                autoScroll: true,
                buttonAlign: 'center',
                bodyStyle: 'padding:0;margin:0',
                itemCls: 'x-form-item-filter',
                iconCls: iconClsContact,
                region: 'center',
                title: '&nbsp;' + contact.name,
                labelWidth: 150,
                frame: true,
                items: [
                    {
                        layout: 'column',
                        defaults: {
                            layout: 'form',
                            columnWidth: 0.45,
                            defaults: {
                                triggerAction: 'all'
                            }
                        },
                        items: [
                            {
                                items: [formCompany,formBranch,formDomain,formSubDomain,formPhone, formMobil, formObs,
                                    {
                                        xtype : "xcheckbox",                                        
                                        fieldLabel: 'Activat',
                                        ctCls: 'x-form-text-mtp',
                                        name: 'Available'
                                    }     ,
                                    formCreated, formModify,formCreatedUser, formModifyUser
                                ]
                            },
                            {
                                items: [formLName, formFName,formSex, formBirth, formAge, formIncome,  formEmail, formEmail2, formWeb, formPosition, formSubPosition, formDivision, formPriority
                                ]
                            }
                        ]
                    },
                    {
                        layout: 'column',
                        defaults: {
                            layout: 'form',
                            columnWidth: 0.45
                        }
                    }
                ],
                buttons: [
                    {
                        text: 'Salveaza',
                        formBind: true,
                        iconCls: 'icon-disk',
                        handler: function() {

                            fpContact.getForm().submit({
                                url: 'Contact/SaveContact/' ,
                                params: {
                                    person_id: contact.id
                                },
                                success: function() {
                                    //Portal.oCenterRegion.remove("contact_" + contact.id, true);
                                }
                            });
                        }
                    },
                    {
                        text: 'Reset',
                        iconCls: 'icon-link-break',
                        handler: function() {
                            Portal.contactForm.fnBringContact(fpContact, contact);
                        }
                    }  ,
                    {
                        text: 'Adauga Contact ',
                        iconCls: 'icon-add',
                        handler: function() {
                            var contact = {
                                id: "new_" + parseInt(new Date().getTime()),
                                name: "Contact",
                                sex: 0
                            }
                            //Portal.main.fnOpenContactScreen(contact);
                        }
                    }
                ]
            });

            fpContact.on({
                afterlayout: {
                    scope: this,
                    single: true,
                    fn: function() {
                        Portal.contactForm.fnBringContact(fpContact, contact);
                    }
                }
            });
            Portal.oCenterRegion.add(fpContact);
        }
    }
});